package com.gzsxy.esjy.service.product.controller;

import com.gzsxy.esjy.bus.log.annotation.OperLog;
import com.gzsxy.esjy.bus.log.enum1.LogActionType;
import com.gzsxy.esjy.service.base.dto.request.AttributeRequest;
import com.gzsxy.esjy.service.base.dto.response.AttributeRsp;
import com.gzsxy.esjy.service.base.dto.response.DefaultResponse;
import com.gzsxy.esjy.service.product.entity.vo.AttributeQueryVo;
import com.gzsxy.esjy.service.product.service.AttributeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xiaolong
 * @version 1.0
 * @description: 商品参数
 * @date 2022/1/10 19:43
 */
@CrossOrigin  //解决跨域
@RefreshScope    //动态刷新配置文件-外部配置文件
@RestController
@RequestMapping("/admin/product/attribute")
@Api("商品参数管理")
public class AttributeController {

    @Autowired
    private AttributeService attributeService;

    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    @ApiOperation("测试配置中心刷新")
    public String getConfigInfo() {
        return configInfo;
    }


    @GetMapping("/{catId}")
    @ApiOperation("根据三级分类id查询对应的参数")
    public DefaultResponse getAttributeByCategoryId(@ApiParam(value = "分类id",required = true) @PathVariable String catId,
                                                    @ApiParam(value = "参数类型",required = true) @RequestParam String attrSel){
       List<AttributeRsp> attributeRsp =  attributeService.getAttributeByCategoryId(catId,attrSel);
        return DefaultResponse.ok(attributeRsp);
    }

    @GetMapping("")
    @ApiOperation("根据参数id查询对应的参数")
    public DefaultResponse getAttributeById(AttributeQueryVo attributeQueryVo){
        AttributeRsp attributeRsp = attributeService.getAttributeById(attributeQueryVo);
        return DefaultResponse.ok(attributeRsp);
    }


    @PostMapping("add")
    @ApiOperation("添加参数")
    @OperLog(operModul = "商品参数管理",operType = LogActionType.ADD,operDesc = "添加参数")
    public DefaultResponse addAttribute(@RequestBody AttributeRequest attributeRequest){
        attributeService.addAttribute(attributeRequest);
        return DefaultResponse.ok();
    }

    @PutMapping("edit")
    @ApiOperation("修改参数")
    @OperLog(operModul = "商品参数管理",operType = LogActionType.UPDATE,operDesc = "修改参数")
    public DefaultResponse updateAttribute(@RequestBody AttributeRequest attributeRequest){
        attributeService.updateAttribute(attributeRequest);
        return DefaultResponse.ok();
    }


    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除参数")
    @OperLog(operModul = "商品参数管理",operType = LogActionType.DELETE,operDesc = "删除参数")
    public DefaultResponse deleteAttribute(@ApiParam(value = "分类id",required = true) @PathVariable String id){
        attributeService.deleteAttribute(id);
        return DefaultResponse.ok();
    }


}
